package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.UserLikeApi;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public long findloveCount(Long id) {

        Query query = new Query(Criteria.where("userId").is(id));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public long findfanCount(Long id) {
        Query query = new Query(Criteria.where("likeUserId").is(id));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public PageBeanVo findUserListList(Integer page, Integer pagesize, Long id) {

        Query query = new Query(Criteria.where("userId").is(id));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageBeanVo(page,pagesize,count,userLikeList);
    }

    @Override
    public PageBeanVo findUserList(Integer page, Integer pagesize, Long id) {
        Query query = new Query(Criteria.where("likeUserId").is(id));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageBeanVo(page,pagesize,count,userLikeList);
    }

    @Override
    public void saveFans(UserLike userLike) {
        mongoTemplate.insert(userLike);
    }

    @Override
    public void deleteUserLike(Long id, Integer uid) {

        Query query = new Query(Criteria.where("userId").is(id).and("likeUserId").is(uid));
        mongoTemplate.remove(query,"user_like");
    }
}
